package com.nti56.springboot.web;

import com.nti56.springboot.interceptor.DictCacheInterceptor;
import com.nti56.springboot.interceptor.OrgCacheInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    /**
     * 解决ie responsebody返回json的时候提示下载问题
     * 
     * @return
     */
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
        MediaType media = new MediaType(MediaType.TEXT_HTML, Charset.forName("UTF-8"));
        supportedMediaTypes.add(media);
        jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
        return jsonConverter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(customJackson2HttpMessageConverter());
    }
    
    /**
     * 重写增加自定义拦截器的注册，某一个拦截器需要先注册进来，才能工作
     */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {	
		//组织机构拦截器
		registry.addInterceptor(new OrgCacheInterceptor()).addPathPatterns("/org/deleteOrg")
		                                                  .addPathPatterns("/org/addOrg");
		//数据字典拦截器
		registry.addInterceptor(new DictCacheInterceptor()).addPathPatterns("/dict/deleteDict")
        												   .addPathPatterns("/dict/addDict");
		WebMvcConfigurer.super.addInterceptors(registry);
	}
    
    
}	
